<template>
  <el-dialog v-el-drag-dialog :title="!form.clientId?'新建客户端':'编辑客户端'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="clientForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.clientId" hidden />
        </el-form-item>
        <el-form-item label="客户端类别" prop="clientType">
          <el-select v-model="form.clientType" :disabled="readonly">
            <el-option
              v-for="item in optionOfType"
              :key="item.dictLabel + '_' + item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="默认客户端" prop="isPrimary">
          <el-radio key="client-primary-0" v-model="form.isPrimary" label="0">是</el-radio>
          <el-radio key="client-primary-1" v-model="form.isPrimary" label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="基础路径" prop="basePath">
          <el-input v-model="form.basePath" />
        </el-form-item>
        <el-form-item label="主机地址" prop="domain">
          <el-input v-model="form.domain" />
        </el-form-item>
        <el-form-item label="主机端口" prop="port">
          <el-input-number v-model="form.port" :min="0"/>
        </el-form-item>
        <el-form-item label="endpoint" prop="endpoint">
          <el-input v-model="form.endpoint" />
        </el-form-item>
        <el-form-item label="bucket" prop="bucket">
          <el-input v-model="form.bucket" />
        </el-form-item>
        <el-form-item label="账号" prop="accessKey">
          <el-input v-model="form.accessKey" />
        </el-form-item>
        <el-form-item label="密码" prop="accessSecret">
          <el-input v-model="form.accessSecret" type="password" show-password />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { fileChannelLoad, fileChannelSave } from '@/api/tool/file'
import { dictGroup } from '@/api/system/dict'

export default {
  name: 'FileClientFormDialog',
  data() {
    const validateUniq = (rule, value, callback) => {
      if (value === undefined || value === '') {
        rule.message = '该项不能为空'
        return callback(new Error(rule.message))
      }

      const param = rule.param.replaceParam
      if (this.form.dbKey && param === 'dbKey') {
        return callback()
      }
      rule.param[param] = value
      rule.param.clientId = this.form.clientId
      this.$http.get(rule.url, {
        params: rule.param
      }).then(response => {
        if (response.valid) {
          callback()
        } else {
          callback(new Error(rule.message))
        }
      }).catch(() => {
        callback()
      })
    }

    return {
      visible: false,
      readonly: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        clientId: undefined,
        clientType: undefined,
        isPrimary: undefined,
        basePath: undefined,
        domain: undefined,
        port: 0,
        endpoint: undefined,
        bucket: undefined,
        accessKey: undefined,
        accessSecret: undefined
      },
      rules: {
        clientType: [{
          required: true,
          trigger: 'blur',
          param: {
            replaceParam: 'clientType'
          },
          validator: validateUniq,
          url: `${process.env.VUE_APP_MODULE_ADMIN_API}/api/file/channel/checkUnique`,
          message: '已存在客户端类别'
        }]
      },
      optionOfType: []
    }
  },
  methods: {
    init(clientId) {
      this.hasLoaded = false
      this.form.clientId = clientId || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(clientId)
        this.getDictData()
      })
    },
    loadForm(clientId) {
      if (clientId) {
        this.readonly = true
        fileChannelLoad({ 'id': clientId }).then(response => {
          this.form = this.$api.objectUpdate(this.form, response.param)
        }).catch(() => {})
      }

      // 判断当前用户权限
      this.hasLoaded = this.$perm.hasPermissionOr(['file:channel:edit'])
    },
    getDictData() {
      dictGroup({ client: 'FileStorageType'}).then(response => {
        this.optionOfType = response
      }).catch(e => {})
    },
    onSubmit() {
      this.$refs.clientForm.validate(valid => {
        if (valid) {
          fileChannelSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.clientForm.resetFields()
      this.visible = false
      this.readonly = false
    }
  }
}
</script>
